package com.itheima.controller;

import com.itheima.domain.*;
import com.itheima.domain.query.UserQueryParam;
import com.itheima.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
public class UserController {
    // 使用@RequiredArgsConstructor注解代替@Autowired
    private final UserService userService;

    /**
     * 登录功能
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        LoginResult loginResult = userService.login(user);

        Result result = new Result();
        if(loginResult==null){
            result.setMsg("账号或密码错误");
            result.setCode(404);
            return result;
        }
        //响应结果封装

        result.setData(loginResult);
        result.setCode(200);
        return result;
    }

    /**
     * 分页查询
     */

    @GetMapping
    public PageResult list(UserQueryParam userQueryParam){
        return userService.list(userQueryParam);
    }
}
